from flask import Blueprint, request
from flask_demo.entities import ImgWork
from flask_demo import app_database

img_work_blue_print = Blueprint("img_work_blue_print", __name__, url_prefix="/img_work")


@img_work_blue_print.route("/new", methods=["POST", ])
def post_img_work_new():
    author_id = request.json.get("author_id")
    content = request.json.get("content")
    database_host = request.json.get("database_host")
    database_name = request.json.get("database_name")
    database_username = request.json.get("database_username")
    database_password = request.json.get("database_password")
    title = request.json.get("title")
    success = True
    new_img_work = None
    try:
        new_img_work = ImgWork(
            author_id=int(author_id),
            content=content,
            database_host=database_host,
            database_name=database_name,
            database_username=database_username,
            database_password=database_password,
            published=False,
            title=title
        )
        app_database.session.add(new_img_work)
        app_database.session.commit()
    except:
        print("error: post_img_work_new")
    return {
        "success": success,
        "data": {
            "img_work_id": new_img_work.img_work_id if new_img_work is not None else -1
        }
    }


@img_work_blue_print.route("/update", methods=["post", ])
def post_img_work_update():
    return {
        "success": False
    }


@img_work_blue_print.route("/all", methods=["get", ])
def get_img_work_all():
    img_works = ImgWork.query.all()
    # 可以进行优化但是暂时没有必要
    # img_works = app_database.session.query(
    #     ImgWork.img_work_id,
    #     ImgWork.author_id,
    #     ImgWork.author,
    #     ImgWork.published,
    #     ImgWork.database_host,
    #     ImgWork.database_name
    # ).all()
    # print(img_works)
    return {
        "success": True,
        "data": {
            "dataList": [i.to_dict1() for i in img_works]
            # "dataList": img_works,
            # "dataList": []
        }
    }


# 如果要优化这个接口，可以参考/img_work/all的那个接口
@img_work_blue_print.route("/mine", methods=["get", ])
def get_img_work_mine():
    user_id = request.args.get("user_id")
    success = False
    img_works = []
    if user_id is not None:
        img_works = ImgWork.query.filter(ImgWork.author_id == int(user_id))
        success = True
        pass
    return {
        "success": success,
        "data": {
            "dataList": [i.to_dict() for i in img_works]
        }
    }


@img_work_blue_print.route("/available", methods=["get", ])
def get_img_work_available():
    res = ImgWork.query.filter(ImgWork.published == 1).all()
    return {
        "success": True,
        "data": {
            "dataList": [i.to_dict() for i in res]
        }
    }


@img_work_blue_print.route("/delete", methods=["post", ])
def post_img_work_delete():
    img_work_id = request.json.get("img_work_id")
    success = False
    if(img_work_id is not None):
        try:
            ImgWork.query.filter(ImgWork.img_work_id == int(img_work_id)).delete()
            app_database.session.commit()
            success = True
            pass
        except:
            print("Error: post_img_work_delete: ")
        pass
    return {
        "success": success,
    }


@img_work_blue_print.route("/get", methods=["get", ])
def get_img_work_get():
    img_work_id = request.args.get("img_work_id")
    res = ImgWork.query.filter(ImgWork.img_work_id == img_work_id).first()
    return {
        "success": True,
        "data": res.to_dict()
    }


@img_work_blue_print.route("/publish", methods=["post", ])
def post_img_work_publish():
    img_work_id = request.json.get("img_work_id")
    success = False
    if (img_work_id is not None):
        try:
            ImgWork.query.filter(ImgWork.img_work_id == int(img_work_id)).update({"published": 1})
            app_database.session.commit()
            success = True
            pass
        except:
            success = False
            print("Error: post_img_work_publish: ")
        pass
    return {
        "success": success,
    }


@img_work_blue_print.route("/cancel", methods=["post", ])
def post_img_work_recall():
    img_work_id = request.json.get("img_work_id")
    success = False
    if (img_work_id is not None):
        try:
            ImgWork.query.filter(ImgWork.img_work_id == int(img_work_id)).update({"published": 0})
            app_database.session.commit()
            success = True
            pass
        except:
            success = False
            print("Error: post_img_work_recall: ")
        pass
    return {
        "success": success,
    }